<?php

namespace App\Admin\Extensions\Column;

use Encore\Admin\Admin;
use Encore\Admin\Grid\Displayers\AbstractDisplayer;

class OpenNote extends AbstractDisplayer
{

    public function display(\Closure $callback = null, $btn = '')
    {
        //$callback = $callback->bindTo($this->row);
        //list($latitude, $longitude) = call_user_func($callback);

        $key = $this->getKey();
        $user_id = $this->row->user_id;
        $name = $this->column->getName();

        $token = csrf_token();

        Admin::script($this->script());

        return <<<EOT
<button class="btn btn-xs btn-default grid-open-map" data-key="{$key}" data-toggle="modal" data-target="#grid-modal-{$name}-{$key}">
    $btn
</button>

<div class="modal" id="grid-modal-{$name}-{$key}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span></button>
        <h4 class="modal-title">$btn</h4>
      </div>
      
      <form class="form-horizontal form">
          <div class="modal-body">
            <input type="hidden" name="user_id" value="{$user_id}" />
            <input type="hidden" name="_token" value="{$token}" />
            <div class="form-group">
                <label style="text-align: right;" class="col-sm-2">调整经验值</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <label class="radio-inline">
                            <input type="radio" name="radio_points" value="1" class="minimal" checked>
                            增加
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="radio_points" value="0" class="minimal" >
                            减少
                        </label>
                    </div>
                    <div class="input-group">
                        <input style="width: 100px; text-align: center;" type="text" id="points" name="points" min="1" value="0" class="form-control points" placeholder="输入 大于0的整数">
                    </div>
                </div>
            </div>
            
            <div class="form-group">
                <label style="text-align: right;" class="col-sm-2">备注</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <textarea name="note" style="width: 400px" class="form-control note" rows="5" placeholder="输入 备注"></textarea>
                    </div>
                </div>
            </div>
            
            
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">取消</button>
            <button type="submit" class="btn btn-primary btn-sm">提交</button>
          </div>
      </form>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>
EOT;
    }

    protected function script()
    {
        return <<<EOT

$('.points:not(.initialized)')
.addClass('initialized')
.bootstrapNumber({
    upClass: 'success',
    downClass: 'primary',
    center: true
});

$('.form').on('submit', function (event) {

    event.preventDefault();

    var form = $(this);
    
    $.post('/admin/points/updatePoints',form.serialize(),function(data){
        if (data.status) {
            $.pjax.reload('#pjax-container');
            swal(data.message, '', 'success');
            //toastr.success(data.message);
            closeModal();

        } else {
            swal(data.message, '', 'error');
        }
    });
    
    function closeModal() {
        $('body').removeClass('modal-open');
        $('.modal-backdrop').remove();
    }
    
});


EOT;
    }

}
